package no.ntnu.fp.client;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import no.ntnu.fp.connection.Initializer;
import no.ntnu.fp.connection.ReceiveWorker;
import no.ntnu.fp.model.Employee;
import no.ntnu.fp.net.co.Connection;

public class Client {
	private static String HOST_NAME = 
		System.getProperty("os.name").equals("Linux") ? "127.0.0.1": "192.168.226.1";//"129.241.127.23";//"78.91.2.17"; //"192.168.226.1";
		//System.getProperty("os.name").equals("Linux") ? "78.91.2.17":"192.168.226.1";
	private static int SERVER_PORT = 33299;
	private static int PORT_NUMBER = 31402;
	private static InetAddress SERVER_ADDRESS;
	private static Employee employee;
	
	public static void setEmployee(Employee employee){
		Client.employee = employee;
	}
	public static Employee getEmployee(){
		return Client.employee;
	}
	
	public static String getHOST_NAME() {
		return HOST_NAME;
	}
	public static void setHOST_NAME(String hostName) {
		HOST_NAME = hostName;
	}
	public static void setSERVER_PORT(int serverPort) {
		SERVER_PORT = serverPort;
	}
	public static void setPORT_NUMBER(int portNumber) {
		PORT_NUMBER = portNumber;
	}

	private static ExecutorService receiveWorkerExecutor = Executors.newSingleThreadExecutor();
	
	public static int getServerPort() {
		return SERVER_PORT;
	}
	
	public static InetAddress getServerAddress() {
		return SERVER_ADDRESS;
	}

	public static int getPortNumber() {
		return PORT_NUMBER;
	}
	
	public static void startReceiveWorker(Connection initializedConnection) {
		if (receiveWorkerExecutor.isShutdown()) {
			//TODO: Handle this in a more elegant fashion. 
			//Maybe let receive worker execute in a Callable and cancel it with Future?
			receiveWorkerExecutor = Executors.newSingleThreadExecutor();
		}
		receiveWorkerExecutor.execute(new ReceiveWorker(initializedConnection));
	}
	
	public static void stopReceiveWorker() {
		receiveWorkerExecutor.shutdownNow();
	}
	
	public static void main(String[] args) throws UnknownHostException, InterruptedException, ExecutionException {
		SERVER_ADDRESS = InetAddress.getByName(HOST_NAME);
		Initializer initializer = new InitializerImpl();
		initializer.initialize();
	}
}
